//
//  NSPVPopView.m
//  Meboo
//
//  Created by kyo on 2024/5/31.
//

#import "NSPVPopView.h"
#import "NSChatPPVCell.h"
#import "KLPaymentManager.h"
#import "NSProductModel.h"
#import "NSFCPPPVBuyPopView.h"
@interface NSPVPopView ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIImageView *typeImageView;
@property (nonatomic, strong) UIImageView *emojiImageView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIButton *continueBtn;

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *subTitleLab;

@property (nonatomic , strong) UICollectionView *collectionView;
//ppv购买成功
@property (nonatomic, strong) NSFCPPPVBuyPopView *buyPopView;
//product
@property (nonatomic, copy) NSString *productID;
@property (nonatomic, assign) NSInteger buyNum;
@end

@implementation NSPVPopView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        
        [[MBMineDataManager sharedManager] getMemberProductWithType:4 success:^(BOOL success) {
            if (success && [MBMineDataManager sharedManager].pvProductArr.count > 0) {
                NSProductModel *model = [MBMineDataManager sharedManager].pvProductArr[0];
                model.isSelect = YES;
                self.productID = model.productCode;
                self.buyPopView.num = [model.benefitNum integerValue];
                self.buyNum = [model.benefitNum integerValue];
                [[MBMineDataManager sharedManager].pvProductArr replaceObjectAtIndex:0 withObject:model];
                [self.collectionView reloadData];
            }
        }];
    }
    return self;
}

#pragma mark - Public Method

#pragma mark - Action
- (void)closeAction {
    self.hidden = YES;
//    [self removeFromSuperview];
}

- (void)nextAction {
    __WeakSelf
    self.backView.userInteractionEnabled = NO;
    MBUserAccount *account = [MBUserAccount defaultAccount];
    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
        self.backView.userInteractionEnabled = YES;
        if (payState) {
            weakSelf.buyPopView.type = PVBuySuccessPop;
            weakSelf.hidden = YES;
//            [weakSelf removeFromSuperview];
            weakSelf.buyPopView.hidden = NO;
            dispatch_async(dispatch_get_main_queue(), ^{
                account.pvNum += self.buyNum;
                [MBUserAccount saveAccount:account];
            });
            if (weakSelf.payBlock) {
                weakSelf.payBlock();
            }
        }
    };
}

#pragma mark -- collectionView  delegate datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [MBMineDataManager sharedManager].pvProductArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSChatPPVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NSChatPPVCell" forIndexPath:indexPath];
    cell.model = [MBMineDataManager sharedManager].pvProductArr[indexPath.row];
    return cell;
}

//设置每个section的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(MBHeightScale(0), MBWidthScale(0), MBHeightScale(0), MBWidthScale(0));
}

//设置列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return MBHeightScale(9);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return MBWidthScale(0);
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth-MBWidthScale(78+56),MBHeightScale(73));
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (int i = 0; i<[MBMineDataManager sharedManager].pvProductArr.count; i++) {
        NSProductModel *model = [MBMineDataManager sharedManager].pvProductArr[i];
        if (i == indexPath.row) {
            model.isSelect = YES;
            self.productID = model.productCode;
            self.buyNum = [model.benefitNum integerValue];
        } else {
            model.isSelect = NO;
        }
        [[MBMineDataManager sharedManager].pvProductArr replaceObjectAtIndex:i withObject:model];
    }
    [self.collectionView reloadData];
}

#pragma mark - UI
- (void)setupUI {
    self.hidden = YES;
    self.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0.5];
    
    [self addSubview:self.backView];
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(39));
//        make.height.mas_equalTo((ScreenWidth-MBHeightScale(78))/297*482);
        make.center.equalTo(self);
    }];
    
//    [self.backView addSubview:self.imageView];
//    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.backView);
//    }];
    
    [self.backView addSubview:self.typeImageView];
    [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.mas_equalTo(MBHeightScale(25));
        make.width.height.mas_offset(MBHeightScale(68));
    }];
    
    [self.backView addSubview:self.emojiImageView];
    [self.emojiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.typeImageView.mas_right).offset(MBHeightScale(-5));;
        make.centerY.equalTo(self.typeImageView.mas_top).offset(MBHeightScale(8));
        make.width.height.mas_offset(MBHeightScale(36));
    }];
    
    [self.backView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeImageView.mas_bottom).offset(MBHeightScale(15));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self.backView addSubview:self.subTitleLab];
    [self.subTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(MBHeightScale(6));
        make.left.mas_equalTo(MBWidthScale(20));
        make.right.mas_equalTo(MBWidthScale(-20));
    }];
    
    [self.backView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitleLab.mas_bottom).offset(MBHeightScale(27));
        make.left.mas_equalTo(MBWidthScale(28));
        make.right.mas_equalTo(MBWidthScale(-28));
        make.height.mas_equalTo(MBHeightScale(155));
    }];
    
    [self.backView addSubview:self.continueBtn];
    [self.continueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(28));
        make.right.mas_equalTo(MBWidthScale(-28));
        make.height.mas_equalTo(MBHeightScale(37));
        make.top.equalTo(self.collectionView.mas_bottom).offset(MBHeightScale(30));
        make.bottom.mas_equalTo(MBHeightScale(-20));
    }];
    
    [self.backView addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.backView.mas_right).offset(MBWidthScale(-15));
        make.top.equalTo(self.backView.mas_top).offset(MBWidthScale(20));
        make.width.height.mas_offset(MBHeightScale(18));
    }];
    
    [self.backView layoutIfNeeded];
    NSLog(@"&*&*&*%f,%f",self.backView.bounds.size.width,self.backView.bounds.size.height);
        [self.backView addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.backView);
            make.width.mas_equalTo(self.backView.bounds.size.width);
            make.height.mas_equalTo(self.backView.bounds.size.height);
        }];
    [self.backView sendSubviewToBack:self.imageView];
    
    [kKeyWindow addSubview:self.buyPopView];
}

#pragma mark - Setter && Getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.numberOfLines = 0;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"Unlock Private Videos";
        _titleLab.font = MBHelBoldFontSizeScale(16);
        _titleLab.textColor = HEXS(@"ffffff");
    }
    return _titleLab;
}

- (UILabel *)subTitleLab {
    if (!_subTitleLab) {
        _subTitleLab = [UILabel new];
        _subTitleLab.numberOfLines = 0;
        _subTitleLab.textAlignment = NSTextAlignmentCenter;
        _subTitleLab.text = @"Unlock the video to check out the bestyour crush has to offer.";
        _subTitleLab.font = MBHelFontSizeScale(13);
        _subTitleLab.textColor = HEXS(@"ffffff");
    }
    return _subTitleLab;
}

- (UIButton *)continueBtn {
    if (!_continueBtn) {
        _continueBtn = [UIButton new];
        [_continueBtn setBackgroundColor:HEXS(@"FF7EF2")];
        _continueBtn.layer.cornerRadius = MBHeightScale(37/2);
        [_continueBtn setTitle:@"Continue" forState:0];
        [_continueBtn setTitleColor:HEXS(@"ffffff") forState:0];
        _continueBtn.titleLabel.font = MBHelFontSizeScale(16);
        [_continueBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _continueBtn;
}

- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton new];
        [_closeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_closeBtn setBackgroundImage:ImageWithName(@"flash_chat_close") forState:0];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [UIView new];
        _backView.backgroundColor = UIColor.clearColor;
    }
    return _backView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.userInteractionEnabled = YES;
        _imageView.image = ImageWithName(@"home_pop_pppv");
    }
    return _imageView;
}

- (UIImageView *)typeImageView {
    if (!_typeImageView) {
        _typeImageView = [UIImageView new];
        _typeImageView.image = ImageWithName(@"home_pop_pv_icon");
    }
    return _typeImageView;
}

- (UIImageView *)emojiImageView {
    if (!_emojiImageView) {
        _emojiImageView = [UIImageView new];
        _emojiImageView.image = ImageWithName(@"chat_pop_pp_emoji");
    }
    return _emojiImageView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[NSChatPPVCell class] forCellWithReuseIdentifier:@"NSChatPPVCell"];

    }
    return _collectionView;
}

- (NSFCPPPVBuyPopView *)buyPopView {
    if (!_buyPopView) {
        _buyPopView = [[NSFCPPPVBuyPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    }
    return _buyPopView;
}
@end
